<!--
desc: 商家页面
-->
<template>
  <div class="seller" ref="seller">
    <div class="seller-content">
      <div class="overview">
        <h1 class="title">{{seller.name}}</h1>
        <div class="desc">
          <star :size="36" :score="seller.score"></star>
          <span class="text">({{seller.ratingCount}})</span>
          <span class="text">月售{{seller.sellCount}}单</span>
        </div>
        <ul class="remark">
          <li class="block">
            <h2>起送价</h2>
            <div class="content">
              <span class="stress">¥{{seller.minPrice}}元</span>
            </div>
          </li>
          <li class="block">
            <h2>商家配送</h2>
            <div class="content">
              <span class="stress">¥ {{seller.deliveryPrice}}元</span>
            </div>
          </li>
          <li class="block">
            <h2>平均配送时间</h2>
            <div class="content">
              <span class="stress">{{seller.deliveryTime}}分钟</span>
            </div>
          </li>
        </ul>
        <!-- 收藏 -->
        <div class="favorite">
          <span class="icon-favorite" @click="toggleFavorite" :class="{'active':favorite}"></span>
          <span class="text">{{favoriteText}}</span>
        </div>
      </div>
      <!-- 分隔组件 -->
      <split></split>
      <div class="bulletin">
        <h1 class="title">公告与活动</h1>
        <div class="content-wrapper">
          <p class="content">{{seller.bulletin}}</p>
        </div>
        <ul v-if="seller.supports" class="supports">
          <li class="support-item" v-for="(item,index) in seller.supports" :key="index">
            <span class="icon" :class="classMap[item.type]"></span>
            <span class="text">{{item.description}}</span>
          </li>
        </ul>
      </div>
      <!-- 分隔组件 -->
      <split></split>
      <!-- 商家实景 -->
      <div class="pics">
        <div class="title">商家实景</div>
        <div class="pic-wrapper" ref="picWrapper">
          <ul class="pic-list" ref="picList">
            <li class="pic-item" v-for="(item,index) in seller.pics" :key="index">
              <img :src="item" width="120" height="120" alt />
            </li>
          </ul>
        </div>
      </div>
      <split></split>
      <div class="info">
        <h2 class="title">商家信息</h2>
        <ul>
          <li class="info-item" v-for="(item,index) in seller.infos" :key="index">{{item}}</li>
        </ul>
      </div>
    </div>
  </div>
</template>
<script>
import { saveToLocal, loadFromLocal } from "../../common/js/store"; // 存储
import star from "../star/star"; // 引入评分组件
import split from "../split/split"; // 引入分隔组件
import Bscroll from "better-scroll";

export default {
  name: "",
  props: {
    seller: {
      type: Object
    }
  },
  components: {
    star,
    split
  },
  watch: {
    seller: function() {
      this._initPics();
    }
  },
  computed: {
    favoriteText() {
      return this.favorite ? "已收藏" : "收藏";
    }
  },
  data() {
    return {
      classMap: [], // 图标类名
      favorite: false, // 收藏状态
      id: "" // 商户id
    };
  },
  methods: {
    _initPics() {
      // 获取相册ul的宽度
      if (this.seller.pics) {
        let picWidth = 120; // 一张图片的宽度
        let margin = 6;
        let width = this.seller.pics.length * (picWidth + margin) - margin; // ul总宽度
        this.$refs.picList.style.width = width + "px";
        this.$nextTick(() => {
          this.picScroll = new Bscroll(this.$refs.picWrapper, {
            scrollX: true // 横向滚动
          });
        });
      }
    },
    // 切换收藏
    toggleFavorite() {
      this.favorite = !this.favorite;
      saveToLocal(this.id, "favorite", this.favorite); // 将商家id存起来
    }
  },
  created() {
    this.id = this.$route.params.id; // 获取地址栏的商家id
    this.classMap = ["decrease", "discount", "special", "invoice", "guarantee"];
    // 初始化滚动条
    this.$nextTick(() => {
      this.scroll = new Bscroll(this.$refs.seller, {
        click: true,
        mouseWheel: true
      });
      // 初始化相册部分滚动条
      this._initPics();
    });
  }
};
</script>
<style lang='stylus' scoped>
.seller {
  position: absolute;
  top: 174px;
  bottom: 0;
  left: 0;
  width: 100%;
  overflow: hidden;

  .overview {
    padding: 18px;

    // 店铺名称
    .title {
      line-height: 14px;
      font-size: 14px;
      color: rgb(7, 17, 27);
      margin-bottom: 8px;
    }

    // 店铺描述
    .desc {
      padding-bottom: 18px;
      font-size: 0;
      border-bottom: 1px solid #ccc;

      // 五星评级
      .star {
        display: inline-block;
        margin-right: 8px;
        vertical-align: top;
      }

      .text {
        display: inline-block;
        line-height: 18px;
        margin-right: 12px;
        font-size: 10px;
        vertical-align: top;
      }
    }

    .remark {
      display: flex;
      padding: 18px 0;

      .block {
        flex: 1;
        text-align: center;
        border-right: 1px solid rgba(7, 17, 27, 0.1);

        &:last-child {
          border: none;
        }

        h2 {
          line-height: 10px;
          font-size: 10px;
          color: rgb(77, 85, 93);
          margin-bottom: 4px;
        }

        .text {
          line-height: 24px;
          font-size: 24px;
          color: rgb(7, 17, 27);
          font-weight: 200;
        }
      }
    }

    // 切换收藏
    .favorite {
      position: absolute;
      top: 18px;
      right: 0px;
      width: 50px;
      text-align: center;

      .icon-favorite {
        display: block;
        margin-bottom: 4px;
        font-size: 24px;
        line-height: 24px;
        color: #d4d6d9;

        &.active {
          color: rgb(240, 20, 20);
        }
      }

      .text {
        line-height: 10px;
        font-size: 10px;
        color: rgb(77, 85, 93);
      }
    }
  }

  // 公告与活动
  .bulletin {
    padding: 18px 18px;

    .title {
      line-height: 14px;
      font-size: 14px;
      color: rgb(7, 17, 27);
      margin-bottom: 8px;
    }

    .content-wrapper {
      padding: 0px 12px 16px 12px;
      border-bottom: 1px solid #ccc;

      .content {
        line-height: 24px;
        font-size: 14px;
        font-weight: 200;
        color: rgb(240, 20, 20);
      }
    }

    .supports {
      margin: 0 auto;

      // 每个li都有padding
      .support-item {
        padding: 16px 12px;
        // margin-bottom: 12px;
        line-height: 16px;
        font-size: 0;
        text-align: left;
        border-bottom: 1px solid #ccc;

        &:last-child {
          margin-bottom: 0;
          border-bottom: none;
        }

        // 每个li前面的图标
        .icon {
          display: inline-block;
          width: 16px;
          height: 16px;
          margin-right: 6px;
          background-size: 16px 16px;

          &.decrease {
            vertical-align: top;
            background-image: url('../header/decrease_2@3x.png');
          }

          &.discount {
            vertical-align: top;
            background-image: url('../header/discount_2@3x.png');
          }

          &.guarantee {
            vertical-align: top;
            background-image: url('../header/guarantee_2@3x.png');
          }

          &.invoice {
            vertical-align: top;
            background-image: url('../header/invoice_2@3x.png');
          }

          &.special {
            vertical-align: top;
            background-image: url('../header/special_2@3x.png');
          }
        }

        .text {
          font-size: 12px;
          line-height: 16px;
          vertical-align: top;
        }
      }
    }
  }

  // 商家实景
  .pics {
    padding: 18px 18px;

    .title {
      line-height: 14px;
      font-size: 14px;
      color: rgb(7, 17, 27);
      margin-bottom: 8px;
    }

    // 图片列表
    .pic-wrapper {
      width: 100%;
      overflow: hidden;
      white-space: nowrap; // 图片不换行

      .pic-list {
        font-size: 0;

        .pic-item {
          display: inline-block;
          width: 120px;
          height: 90px;
          margin-right: 6px;

          &:last-child {
            margin-right: 0;
          }
        }
      }
    }
  }

  // 商家信息
  .info {
    padding: 18px 18px;

    .title {
      line-height: 14px;
      font-size: 14px;
      color: rgb(7, 17, 27);
      margin-bottom: 8px;
    }

    .info-item {
      padding: 16px 12px;
      border-top: 1px solid rgba(7, 17, 27, 0.1);
      font-size: 12px;
      line-height: 16px;
      font-weight: 200;
      color: rgb(7, 17, 27);
    }
  }
}
</style>
